package g2.routemaster.gui.contents;

import g2.routemaster.gui.BoardSingleton;
import g2.routemaster.gui.GManager;
import g2.routemaster.model.Administrator;
import g2.routemaster.model.Cartwright;
import g2.routemaster.model.GameManager;
import g2.routemaster.model.Officials;
import g2.routemaster.model.PostalCarrier;
import g2.routemaster.model.Postmaster;
import g2.routemaster.model.TurnManager;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;

public class OfficialsGroup extends Composite {
	private CLabel lAdministrator;

	private CLabel lCartwright;

	private CLabel lPostalCarrier;

	private CLabel lPostmaster;

	private Button bUse;

	private Group officialsGroup;

	Image imageAdministrator;

	Image imageCartwright;

	Image imagePostalCarrier;

	Image imagePostmaster;

	Image imageAdministratorDisabled;

	Image imageCartwrightDisabled;

	Image imagePostalCarrierDisabled;

	Image imagePostmasterDisabled;

	public OfficialsGroup(Composite parent, int style) {
		super(parent, style);
		imageAdministrator = new Image(Display.getCurrent(), getClass()
				.getResourceAsStream("/pic/aid_administrator.png"));
      imageAdministratorDisabled = new Image(Display.getCurrent(), getClass()
            .getResourceAsStream("/pic/aid_administrator_disabled.png"));
      imagePostmaster = new Image(Display.getCurrent(), getClass()
            .getResourceAsStream("/pic/aid_postmaster.png"));
      imagePostmasterDisabled = new Image(Display.getCurrent(), getClass()
            .getResourceAsStream("/pic/aid_postmaster_disabled.png"));
		imagePostalCarrier = new Image(Display.getCurrent(), getClass()
				.getResourceAsStream("/pic/aid_postalcarrier.png"));
      imagePostalCarrierDisabled = new Image(Display.getCurrent(), getClass()
            .getResourceAsStream("/pic/aid_postalcarrier_disabled.png"));
      imageCartwright = new Image(Display.getCurrent(), getClass()
            .getResourceAsStream("/pic/aid_cartwright.png"));
      imageCartwrightDisabled = new Image(Display.getCurrent(), getClass()
            .getResourceAsStream("/pic/aid_cartwright_disabled.png"));
	}

	public void create() {
		officialsGroup = new Group(this, SWT.NONE);
		officialsGroup.setText("Officials");
		GridLayout layout = new GridLayout();
		layout.numColumns = 3;
		layout.verticalSpacing = 3;
		layout.horizontalSpacing = 3;
		layout.marginWidth = 0;
		layout.makeColumnsEqualWidth = true;
		officialsGroup.setLayout(layout);

		Cursor handCursor = new Cursor(getDisplay(), SWT.CURSOR_HAND);

		lAdministrator = new CLabel(officialsGroup, SWT.SHADOW_OUT);
		lAdministrator.setData(new Administrator());
		lAdministrator.setImage(imageAdministrator);
		lAdministrator.setToolTipText("Administrator");
		lAdministrator.setCursor(handCursor);
		lAdministrator.addListener(SWT.MouseDown, cardSelectListener);
		lAdministrator.addListener(SWT.MouseDoubleClick, new Listener() {
			public void handleEvent(Event e) {
				useOfficial(new Administrator());
			}
		});

		lPostmaster = new CLabel(officialsGroup, SWT.SHADOW_OUT);
		lPostmaster.setData(new Postmaster());
		lPostmaster.setImage(imagePostmaster);
		lPostmaster.setToolTipText("Postmaster");
		lPostmaster.setCursor(handCursor);
		lPostmaster.addListener(SWT.MouseDown, cardSelectListener);
		lPostmaster.addListener(SWT.MouseDoubleClick, new Listener() {
			public void handleEvent(Event e) {
				useOfficial(new Postmaster());
			}
		});

		lPostalCarrier = new CLabel(officialsGroup, SWT.SHADOW_OUT);
		lPostalCarrier.setData(new PostalCarrier());
		lPostalCarrier.setImage(imagePostalCarrier);
		lPostalCarrier.setToolTipText("Postal Carrier");
		lPostalCarrier.setCursor(handCursor);
		lPostalCarrier.addListener(SWT.MouseDown, cardSelectListener);
		lPostalCarrier.addListener(SWT.MouseDoubleClick, new Listener() {
			public void handleEvent(Event e) {
				useOfficial(new PostalCarrier());
			}
		});

		new Label(officialsGroup, SWT.NONE);

		lCartwright = new CLabel(officialsGroup, SWT.SHADOW_OUT);
		lCartwright.setData(new Cartwright());
		lCartwright.setImage(imageCartwright);
		lCartwright.setToolTipText("Cartwright");
		lCartwright.setCursor(handCursor);
		lCartwright.addListener(SWT.MouseDown, cardSelectListener);
		lCartwright.addListener(SWT.MouseDoubleClick, new Listener() {
			public void handleEvent(Event e) {
				useOfficial(new Cartwright());
			}
		});

		bUse = new Button(officialsGroup, SWT.PUSH);
		bUse.setText("Use Official");
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 3;
		bUse.setLayoutData(gd);
		bUse.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				useOfficial(getSelectedOfficial());
			}
		});
	}

	void useOfficial(Officials official) {
		GameManager gm = GManager.instance().getGameManager();
		TurnManager tm = gm.getTurnManager();
		tm.chooseOfficial(official);

		BoardSingleton.instance().updateAll();
	}

	public void updateData() {
		GameManager gm = GManager.instance().getGameManager();
		TurnManager tm = gm.getTurnManager();
		Officials usedOfficial = tm.getOfficials();
		if (usedOfficial != null) {
			if (usedOfficial instanceof Postmaster) {
				lPostmaster.setBackground(getSelectedColor());
			} else if (usedOfficial instanceof Administrator) {
				lAdministrator.setBackground(getSelectedColor());
			} else if (usedOfficial instanceof PostalCarrier) {
				lPostalCarrier.setBackground(getSelectedColor());
			} else if (usedOfficial instanceof Cartwright) {
				lCartwright.setBackground(getSelectedColor());
			}
			lPostmaster.setEnabled(false);
			lPostmaster.setImage(imagePostmasterDisabled);
			lAdministrator.setEnabled(false);
			lAdministrator.setImage(imageAdministratorDisabled);
			lPostalCarrier.setEnabled(false);
			lPostalCarrier.setImage(imagePostalCarrierDisabled);
			lCartwright.setEnabled(false);
			lCartwright.setImage(imageCartwrightDisabled);
			bUse.setEnabled(false);
		} else {
			boolean canUse = tm.canChooseOfficial(new Administrator());
			lAdministrator.setEnabled(canUse);
			if (canUse)
				lAdministrator.setImage(imageAdministrator);
			else
				lAdministrator.setImage(imageAdministratorDisabled);

			canUse = tm.canChooseOfficial(new Postmaster());
			lPostmaster.setEnabled(canUse);
			if (canUse)
				lPostmaster.setImage(imagePostmaster);
			else
				lPostmaster.setImage(imagePostmasterDisabled);

			canUse = tm.canChooseOfficial(new PostalCarrier());
			lPostalCarrier.setEnabled(canUse);
			if (canUse)
				lPostalCarrier.setImage(imagePostalCarrier);
			else
				lPostalCarrier.setImage(imagePostalCarrierDisabled);

			canUse = Cartwright.canUse(tm);
			lCartwright.setEnabled(canUse);
			if (canUse)
				lCartwright.setImage(imageCartwright);
			else
				lCartwright.setImage(imageCartwrightDisabled);
			bUse.setEnabled(true);
		}
	}

	Color getSelectedColor() {
		return getDisplay().getSystemColor(SWT.COLOR_YELLOW);
	}

	Color getUnselectedColor() {
		return getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
	}

	void unselectPlace(CLabel label) {
		label.setBackground(getUnselectedColor());
	}

	void selectPlace(CLabel label) {
		label.setBackground(getSelectedColor());
	}

	Officials getSelectedOfficial() {
		for (Control child : officialsGroup.getChildren()) {
			if (child instanceof CLabel) {
				CLabel cardPlace = (CLabel) child;
				if (cardPlace.getBackground().equals(getSelectedColor())) {
					return (Officials) cardPlace.getData();
				}
			}
		}
		return null;
	}

	Listener cardSelectListener = new Listener() {
		public void handleEvent(Event e) {
			CLabel item = (CLabel) e.widget;

			if (item.getBackground().equals(getSelectedColor())) {
				unselectPlace(item);
			} else {
				for (Control place : officialsGroup.getChildren()) {
					if (place instanceof CLabel) {
						unselectPlace((CLabel) place);
					}
				}
				selectPlace(item);
			}
		}
	};
}
